package com.wwc.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwc.business.controller.dto.station.StationDeleteDTO;
import com.wwc.business.controller.dto.station.StationInsertDTO;
import com.wwc.business.controller.dto.station.StationSelectListDTO;
import com.wwc.business.controller.dto.station.StationUpdateDTO;
import com.wwc.business.controller.vo.StationSelectListVO;
import com.wwc.business.entity.Station;
import com.wwc.business.mapper.StationMapper;
import com.wwc.business.service.IStationService;
import com.wwc.common.exception.BusinessException;
import com.wwc.common.exception.BusinessExceptionEnum;
import com.wwc.common.response.PageResponse;
import com.wwc.common.utils.SnowUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements IStationService{
	@Resource
	private StationMapper stationMapper;

	@Override
	public void insert(StationInsertDTO stationInsertDTO){
		// 根据查询条件查询车站列表
		QueryWrapper<Station> stationQueryWrapper = new QueryWrapper<>();
		stationQueryWrapper.eq("name", stationInsertDTO.getName());
		List<Station> stationList = stationMapper.selectList(stationQueryWrapper);
		// 如果车站已存在则抛出异常
		if(CollUtil.isNotEmpty(stationList)){
			throw new BusinessException(BusinessExceptionEnum.BUSINESS_STATION_NAME_UNIQUE_ERROR);
		}

		// 新增车站
		Station station = BeanUtil.copyProperties(stationInsertDTO, Station.class);
		station.setId(SnowUtil.getSnowflakeNextId());
		station.setCreateTime(new Date());
		station.setUpdateTime(new Date());
		stationMapper.insert(station);
	}

	@Override
	public void update(StationUpdateDTO stationUpdateDTO){
		// 判断想要修改的数据是否存在
		Station s = stationMapper.selectById(stationUpdateDTO.getId());
		if(ObjectUtil.isNull(s)){
			throw new BusinessException(BusinessExceptionEnum.MODIFIED_DATA_DOES_NOT_EXIST);
		}

		// 根据查询条件查询车站列表
		QueryWrapper<Station> stationQueryWrapper = new QueryWrapper<>();
		stationQueryWrapper.eq("name", stationUpdateDTO.getName());
		stationQueryWrapper.ne("id", stationUpdateDTO.getId());
		List<Station> stationList = stationMapper.selectList(stationQueryWrapper);

		// 根据车站名称查询如果为空 或者 车站名称和要修改的车站是同一条数据，反之抛出异常
		if(CollUtil.isNotEmpty(stationList)){
			throw new BusinessException(BusinessExceptionEnum.BUSINESS_STATION_NAME_UNIQUE_ERROR);
		}

		// 修改车站
		Station station = BeanUtil.copyProperties(stationUpdateDTO, Station.class);
		station.setUpdateTime(new Date());
		stationMapper.updateById(station);
	}

	@Override
	public void delete(StationDeleteDTO stationDeleteDTO){
		// 判断想要删除的数据是否存在
		Station s = stationMapper.selectById(stationDeleteDTO.getId());
		if(ObjectUtil.isNull(s)){
			throw new BusinessException(BusinessExceptionEnum.MODIFIED_DELETE_DATA_DOES_NOT_EXIST);
		}

		// 删除车站
		stationMapper.deleteById(stationDeleteDTO.getId());
	}

	@Override
	public PageResponse<StationSelectListVO> selectListPage(StationSelectListDTO stationSelectListDTO){
		// 根据查询条件和分页配置查询车站分页列表
		Page<Station> page = new Page<>(stationSelectListDTO.getPage(), stationSelectListDTO.getSize());
		QueryWrapper<Station> stationQueryWrapper = new QueryWrapper<>();
		stationQueryWrapper.orderByAsc("name_pinyin");
		Page<Station> stationPage = stationMapper.selectPage(page, stationQueryWrapper);

		// 返回自定义分页配置类
		PageResponse<StationSelectListVO> pageResponse = new PageResponse<>();
		pageResponse.setTotal(stationPage.getTotal());
		pageResponse.setList(BeanUtil.copyToList(stationPage.getRecords(), StationSelectListVO.class));
		return pageResponse;
	}

	@Override
	public List<StationSelectListVO> selectList(){
		// 根据条件查询车站列表
		QueryWrapper<Station> stationQueryWrapper = new QueryWrapper<>();
		stationQueryWrapper.orderByAsc("name_pinyin");
		List<Station> stationList = stationMapper.selectList(stationQueryWrapper);

		// 返回车站列表VO类
		return BeanUtil.copyToList(stationList, StationSelectListVO.class);
	}
}
